package com.txl.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.txl.common.AjaxResult;
import com.txl.pojo.TbUser;
import com.txl.service.TbUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import javax.servlet.http.HttpServletRequest;
import javax.validation.constraints.NotNull;
import java.util.Date;
import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;
import javax.swing.*;
import java.awt.*;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.List;
import java.util.Timer;

@RestController
public class TbUserController {

    @Autowired
    private TbUserService tbUserService;

    @GetMapping("/")
    public String index() {
        return "Heelo World!";
    }

    @GetMapping("/test/{id}")
    public AjaxResult test(@PathVariable("id") Long id) {
        TbUser tbUser = new TbUser();
        tbUser.setId(id);
        return AjaxResult.success(tbUserService.getTbUserList(tbUser));
    }

    @GetMapping("/list")
    public AjaxResult getUserList() {
        return AjaxResult.success(tbUserService.getUserList());
    }

    @PutMapping
    @GetMapping("/page")
    public AjaxResult testPageHelp() {
        return AjaxResult.success(tbUserService.queryUsers(0, 10));
    }

    @GetMapping("/thread")
    public void thread() {
        ThreadPoolExecutor threadPool = new ThreadPoolExecutor(5, 10, 10L, TimeUnit.SECONDS, new ArrayBlockingQueue(10));
        Runnable task = () -> System.out.println("执行任务啦");

        Runnable task2 = new Runnable() {
            public void run() {
                System.out.println("执行任务啦");
            }
        };
        task.run();
        System.out.println("\"我\" == \"你爹\"");
        System.out.println(true);
        threadPool.execute(task);
//        new Thread(new ThreadDemo(1)).start();
//        new Thread(new ThreadDemo(2)).start();
//        new Thread(new ThreadDemo(3)).start();
//        new Thread(new ThreadDemo(4)).start();
//        new Thread(new ThreadDemo(5)).start();
    }

    @GetMapping("/stream")
    public AjaxResult stream() {
        return AjaxResult.success(tbUserService.getUserListByStream());
    }

    @GetMapping("/static")
    public AjaxResult testStatic() {
        return AjaxResult.success(tbUserService.testStatic());
    }

    @RequestMapping("/test")
    public AjaxResult test(@NotNull @RequestParam(value = "file", required = false) MultipartFile file, @NotNull @RequestParam(value = "json", required = false) String jsonStr) {
        JSONObject jsonObject = null;
        try {
            jsonObject = JSON.parseObject(jsonStr);
        } catch (Exception e) {
            return AjaxResult.error("参数:（" + jsonStr + "）不是Json格式");
        }
        if (!StringUtils.isEmpty(jsonObject) && !StringUtils.isEmpty(jsonObject.get("size")) && !StringUtils.isEmpty(file)) {
            if ((Long) jsonObject.get("size") == file.getSize()) return AjaxResult.success("大小相同");
            else return AjaxResult.error("大小不同");
        }
        return AjaxResult.error("请检查参数格式");
    }

    @RequestMapping("/ip")
    public String testIp(HttpServletRequest request) {
        String localAddr = request.getLocalAddr();
        String a ="\u725b\u903c";
        System.out.println(a);
        return localAddr;
    }
}




